package com.service;

import com.bean.PlanInfo;
import com.bean.QuestionInfo;
import com.bean.other.MetaInfo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mapper.PlanInfoMapper;
import com.mapper.QuestionInfoMapper;
import com.mapper.repository.PlanInfoRepository;
import com.mapper.repository.QuestionInfoRepository;
import com.service.impl.PlanInfoService;
import com.service.impl.QuestionInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.UUID;

@Service
public class QuestionInfoServiceImpl implements QuestionInfoService {

    @Autowired
    private QuestionInfoRepository questionInfoRepository;

    @Autowired
    private QuestionInfoMapper questionInfoMapper;

    @Override
    public String insert(QuestionInfo questionInfo) {
        questionInfo.setId(UUID.randomUUID().toString());
        questionInfo.setCreatetime(LocalDateTime.now());
        questionInfoRepository.save(questionInfo);
        return "新增成功";
    }

    @Override
    public String update(QuestionInfo questionInfo) {
        questionInfo.setUpdatetime(LocalDateTime.now());
        questionInfoRepository.save(questionInfo);
        return "更新成功";
    }

    @Override
    public String delete(QuestionInfo questionInfo) {
        questionInfoRepository.delete(questionInfo);
        return "删除成功";
    }

    @Override
    public PageInfo<QuestionInfo> select(QuestionInfo questionInfo, MetaInfo metaInfo) {
        //PageHelper.startPage(metaInfo.getPage(),metaInfo.getSize());
        PageHelper.startPage(metaInfo.getPage(),metaInfo.getSize(),"createtime");
        List<QuestionInfo> list=questionInfoMapper.select(questionInfo);
        PageInfo<QuestionInfo> pageInfo=new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public QuestionInfo get(String id) {
        Optional<QuestionInfo> optional=questionInfoRepository.findById(id);
        if(optional.isPresent()){
            return optional.get();
        }
        return null;
    }
}
